package com.dataextend.service;

import com.alibaba.fastjson.JSONObject;
import com.dataextend.mapper.cluster.ReportKpiMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *  用户画像 —— KPI报表
 */
@Service
public class ReportKpiService {
    @Autowired
    ReportKpiMapper reportKpiMapper;

    // 解析日期从String到Long
    private long parseDateToLong (String dataDate){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            long dateTime = simpleDateFormat.parse(dataDate).getTime();
            return dateTime;
        } catch (Exception e){
            e.printStackTrace();
        }
        return 0L;
    }

    // 除法计算
    private static String chufa(long param1, long param2){
        DecimalFormat dF = new DecimalFormat("0.00000");
        return dF.format((float) (param1 - param2)/param1 *100);
    }

    // 新增注册用户数   ads_report_kpi_new_regist_num
    public JSONObject newRegistNum(String dataDate){
        JSONObject result = new JSONObject();
        Integer todayNum = reportKpiMapper.newRegistNum(dataDate);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String yesterday = simpleDateFormat.format(new Date(parseDateToLong(dataDate) - 86400000));
        Integer yesterdayNum = reportKpiMapper.newRegistNum(yesterday);
        String ratio = chufa(Long.valueOf(todayNum), Long.valueOf(yesterdayNum));
        result.put("dataDate", dataDate);
        result.put("todayNum", todayNum);
        result.put("ratio", String.format("%.2f", Double.valueOf(ratio)) + "%");
        return result;
    }


    // 日活用户数   ads_report_kpi_live_users
    public JSONObject liveUsers(String dataDate){
        JSONObject result = new JSONObject();
        Integer todayNum = reportKpiMapper.liveUsers(dataDate);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String yesterday = simpleDateFormat.format(new Date(parseDateToLong(dataDate) - 86400000));
        Integer yesterdayNum = reportKpiMapper.liveUsers(yesterday);
        String ratio = chufa(Long.valueOf(todayNum), Long.valueOf(yesterdayNum));
        result.put("dataDate", dataDate);
        result.put("todayNum", todayNum);
        result.put("ratio", String.format("%.2f", Double.valueOf(ratio)) + "%");
        return result;
    }


    // 付费用户数   ads_report_kpi_paid_users
    public JSONObject paidUsers(String dataDate){
        JSONObject result = new JSONObject();
        Integer todayNum = reportKpiMapper.paidUsers(dataDate);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String yesterday = simpleDateFormat.format(new Date(parseDateToLong(dataDate) - 86400000));
        Integer yesterdayNum = reportKpiMapper.paidUsers(yesterday);
        String ratio = chufa(Long.valueOf(todayNum), Long.valueOf(yesterdayNum));
        result.put("dataDate", dataDate);
        result.put("todayNum", todayNum);
        result.put("ratio", String.format("%.2f", Double.valueOf(ratio)) + "%");
        return result;
    }

    // 付费订单量    ads_report_kpi_paid_orders
    public JSONObject paidOrders(String dataDate){
        JSONObject result = new JSONObject();
        Integer todayNum = reportKpiMapper.paidOrders(dataDate);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String yesterday = simpleDateFormat.format(new Date(parseDateToLong(dataDate) - 86400000));
        Integer yesterdayNum = reportKpiMapper.paidOrders(yesterday);
        String ratio = chufa(Long.valueOf(todayNum), Long.valueOf(yesterdayNum));
        result.put("dataDate", dataDate);
        result.put("todayNum", todayNum);
        result.put("ratio", String.format("%.2f", Double.valueOf(ratio)) + "%");
        return result;
    }

    // ------------------------------------------------------------------------------------------------

    // 订单金额   ads_report_kpi_amount
    public JSONObject orderAmount(String startDate, String endDate){
        JSONObject result = new JSONObject();
        List<JSONObject> list = reportKpiMapper.orderAmount(startDate, endDate);
        result.put("info", list);
        return result;
    }

    // 累计用户数  ads_report_kpi_cumulative_users
    public JSONObject cumulativeUsers(String startDate, String endDate){
        JSONObject result = new JSONObject();
        List<JSONObject> list = reportKpiMapper.cumulativeUsers(startDate, endDate);
        result.put("info", list);
        return result;
    }

    // ------------------------------------------------------------------------------------------------

    // 日活用户渠道分布   ads_report_kpi_channel_distribute
    public JSONObject userChannelDistribute(String startDate, String endDate){
        JSONObject result = new JSONObject();
        List<JSONObject> list = reportKpiMapper.userChannelDistribute(startDate, endDate);
        result.put("info", list);
        return result;
    }

    //  日活用户城市分布   ads_report_kpi_city_distribute
    public JSONObject cityDistribute(String startDate, String endDate){
        JSONObject result = new JSONObject();
        List<JSONObject> list = reportKpiMapper.cityDistribute(startDate, endDate);
        result.put("info", list);
        return result;
    }

    //   日活用户平台分布   ads_report_kpi_platform_distribute
    public JSONObject platformDistribute(String startDate, String endDate){
        JSONObject result = new JSONObject();
        List<JSONObject> list = reportKpiMapper.platformDistribute(startDate, endDate);
        result.put("info", list);
        return result;
    }

    //  日活用户新老客分布  ads_report_kpi_newold_distibute
    public JSONObject newoldlDistribute(String startDate, String endDate){
        JSONObject result = new JSONObject();
        List<JSONObject> list = reportKpiMapper.newoldlDistribute(startDate, endDate);
        result.put("info", list);
        return result;
    }

}
